﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Text.RegularExpressions;
using System.Collections;
using System.Reflection.Emit;

namespace FormsAppT8
{
    public partial class BQFROMYL: Form
    {

        //dbconn db = new dbconn();

        public BQFROMYL()
        {
            InitializeComponent(); // 初始化窗体
            //comboBox1.SelectedIndex=0;

        }
      
        //清单界面绑定
        public void bdmain()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;  // 入库单号
            string x2 = textBox12.Text; // 供应商
            string x3 = textBox8.Text;  // 物料代码
            string x4 = textBox7.Text;  // 物料名称
            string x5 = textBox10.Text; // 规格型号

                sql = "select distinct top 100 a.billno as 单据编号,a.billdate as 单据日期,a.bizpartnerid as 供应商编号,c.shortname as 供应商简称,c.BizPartnerName as 供应商名称 from purreceivingorder a,purreceivingorderdetail b,combusinesspartner c,comMaterialGroup d where a.billno=b.billno and a.bizpartnerid=c.bizpartnerid and b.materialid=d.materialid " +
            " and a.billno like '%" + x1 + "%'" +
            " and a.bizpartnerid  like '%" + x2 + "%'" +
            " and d.materialid  like '%" + x3 + "%'"+
             " and d.materialname  like '%" + x4 + "%'"+
              //" and '11'  like '%" + textBox3.Text + "%'" +
             " and d.materialspec  like '%" + x5 + "%' order by billdate desc";

                table = "purreceivingorder";
        
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dbconn db = new dbconn();
            dataGridView1.Rows.Clear();
            db.bdlm(sql, table, dataGridView1);
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Clear();
        }

        //明细界面绑定
        public void bddetail()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;
            string x2 = textBox12.Text;
            string x3 = textBox8.Text;
            string x4 = textBox7.Text;
            string x5 = textBox10.Text;
            string billno = "";
            if (dataGridView1.SelectedRows.Count > 0)
            {

                billno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            }
            sql = "select a.RowNo as 行号,a.MaterialId as 产品编号,b.MaterialName as 产品名称,b.Materialspec as 物料规格,cast( a.SQuantity as float) as 本次入库数量,a.SUnitId as 单位,a.BatchNo as 批号 from (" +
               "select a.billno,a.RowNo,a.MaterialId,a.SQuantity,a.BPMaterialId,a.SUnitId,a.BatchNo from purreceivingorderdetail a " +
               ")a,comMaterialGroup b where a.MaterialId=b.MaterialId " +
               "and a.BillNo='" + billno + "' and b.MaterialId  like '%" + x3 + "%'" + " and b.materialname  like '%" + x4 + "%'" +
             " and b.materialspec  like '%" + x5 + "%'";
                table = "purreceivingorderdetail";
                dataGridView2.Rows.Clear();
                dataGridView2.Columns.Clear();
                dbconn db = new dbconn();
                dataGridView2.Rows.Clear();
                db.bdlm(sql, table, dataGridView2);
        }

        //二维码生成
        public void ewmsc()
        {
            
			string data = textBox15.Text+"|" + textBox9.Text + "|" + textBox4.Text;
                //pid + bh + "|" + textBox15.Text + "|" + textBox4.Text + "|" + textBox16.Text + "|" + textBox2.Text + "|" +( Convert.ToDouble(textBox5.Text)*1000).ToString();
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //设置编码模式 
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
			//设置二维码像素缩放（提高分辨率，避免打印糊成黑块）
			qrCodeEncoder.QRCodeScale = 8;
            //设置编码版本
            qrCodeEncoder.QRCodeVersion = 4;
            //设置编码错误纠正 
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

            Image image;
            //生成二维码图片
			image = qrCodeEncoder.Encode(data, Encoding.ASCII); //Encoding.UTF8);

            pictureBox2.Image = image;
        }

        //标签信息显示
        public void bqxx()
        {
            

        }

        //标签内容置空
        public void bqzk()
        {
            //label50.Text = "";
            label16.Text = "";
            label7.Text = "";
            //label23.Text = "";
            //label37.Text = "";
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {

            //Bitmap _NewBitmap = new Bitmap(300, 300);
            //panel5.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            Pen p = new Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.Black), 1); //线条颜色和粗细
            Pen pp = new Pen(System.Drawing.Color.Black, 0.3F);
            Font MyFont1 = new Font("宋体", 10);
            Font MyFont2 = new Font("宋体", 12, FontStyle.Bold);
            Font MyFont4 = new Font("宋体", 24, FontStyle.Bold);



            ////100*120
            //40*80  联强
            //int xS = 10, xs1 = 40, yS = 5, h = 10, ys1 = 50, ys2 = 60;

            e.Graphics.PageUnit = GraphicsUnit.Millimeter;   //位置度量单位设为毫米
            e.Graphics.TranslateTransform(0F, 0F);  //偏移量
            ////二维码
			// 使用二维码原图，绘制到白色背景，避免透明导致整块发黑；并采用像素级缩放保持清晰
			if (pictureBox2.Image != null)
			{
				Image qrImg = pictureBox2.Image;
				using (Bitmap whiteBg = new Bitmap(qrImg.Width, qrImg.Height, PixelFormat.Format24bppRgb))
				using (Graphics g2 = Graphics.FromImage(whiteBg))
				{
					g2.Clear(System.Drawing.Color.White);
					g2.DrawImage(qrImg, 0, 0, qrImg.Width, qrImg.Height);
					var oldSmoothing = e.Graphics.SmoothingMode;
					var oldInterp = e.Graphics.InterpolationMode;
					var oldPixelOffset = e.Graphics.PixelOffsetMode;
					e.Graphics.SmoothingMode = SmoothingMode.None;
					e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
					e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
					// 以毫米为单位，输出为 35mm x 35mm
					e.Graphics.DrawImage(whiteBg, 2, 43, 35, 35);
					e.Graphics.SmoothingMode = oldSmoothing;
					e.Graphics.InterpolationMode = oldInterp;
					e.Graphics.PixelOffsetMode = oldPixelOffset;
				}
			}
            //e.Graphics.DrawImage(pictureBox1.Image, 35, 20, 30, 10);
            //
            //e.Graphics.DrawString("包装数量", MyFont2, Brushes.Black, 45, 5);
            //e.Graphics.DrawString(label36.Text, MyFont2, Brushes.Black, 40, 12);
            ////画表格横线
            e.Graphics.DrawLine(pp, new Point(0, 41), new Point(40, 41));
            e.Graphics.DrawLine(pp, new Point(1, 0), new Point(1, 80));
            // e.Graphics.DrawLine(pp, new Point(5, 40-6), new Point(75, 40-6));
            //e.Graphics.DrawLine(pp, new Point(5, 45-4), new Point(75, 45-4));
            // e.Graphics.DrawLine(pp, new Point(5, 50-2), new Point(75, 50-2));
            // e.Graphics.DrawLine(pp, new Point(5, 55), new Point(75, 55));

            ////画表格竖线 
            // e.Graphics.DrawLine(pp, new Point(5, 35 - 8), new Point(5, 55));
            // e.Graphics.DrawLine(pp, new Point(20, 35 - 8), new Point(20, 55));
            // e.Graphics.DrawLine(pp, new Point(75, 35 - 8), new Point(75, 55));

            Font MyFont3 = new Font("宋体", 20, FontStyle.Bold);

            e.Graphics.DrawString(label17.Text, MyFont3, Brushes.Black, 4, 3);  // 采购入库
            e.Graphics.DrawString("收货单号:", MyFont1, Brushes.Black, 8, 11);
            e.Graphics.DrawString(label15.Text, MyFont1, Brushes.Black, 7, 15); //收货单号
            //e.Graphics.DrawString(label7.Text, MyFont1, Brushes.Black, 4, 20);  // 物料名称

            string text = label7.Text;
            int maxChars = 10;

            if (text.Length <= maxChars)
            {
                // 不超过10个字符，直接画在(4,20)
                e.Graphics.DrawString(text, MyFont1, Brushes.Black, 4, 20);
            }
            else
            { 
                // 超过10个字符，分两行：第一行(4,20)，第二行(4,25)
                string firstLine = text.Substring(0, maxChars);
                string secondLine = text.Substring(maxChars);
                e.Graphics.DrawString(firstLine, MyFont1, Brushes.Black, 4, 20);
                e.Graphics.DrawString(secondLine, MyFont1, Brushes.Black, 4, 25);
            }

            e.Graphics.DrawString(label18.Text, MyFont2, Brushes.Black, 4, 30); // 数量
            e.Graphics.DrawString(label16.Text, MyFont2, Brushes.Black, 4, 35); // 批号
            
            //e.Graphics.DrawString(label17.Text, MyFont2, Brushes.Black, 30, 15);
            //e.Graphics.DrawString(label17.Text, MyFont1, Brushes.Black, 5, 28);
            //e.Graphics.DrawString("物料编码:", MyFont1, Brushes.Black, 5, 33);
            //e.Graphics.DrawString("物料名称:", MyFont1, Brushes.Black, 5, 38);
            //e.Graphics.DrawString("物料规格:", MyFont1, Brushes.Black, 5, 43);
            //e.Graphics.DrawString("入库日期:", MyFont1, Brushes.Black, 15, 43);
            
            //e.Graphics.DrawString("数量:", MyFont1, Brushes.Black, 5, 50 - 1);
            //e.Graphics.DrawString(label50.Text, MyFont1, Brushes.Black, 20, 33);

            
            

            
            // e.Graphics.DrawString(label23.Text, MyFont1, Brushes.Black, 13, 50 - 1);

            //e.Graphics.DrawString("图号：" + label17.Text, MyFont3, Brushes.Black, 5, 55);
        }

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {

        }

        private void FormT8BQ_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bdmain();
            bddetail();
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            //label50.Text = textBox15.Text;
            ewmsc();

        }

        //private void textBox17_TextChanged(object sender, EventArgs e)
        //{
        //    label16.Text = textBox17.Text;
        //    ewmsc();
        //}

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            label16.Text = textBox4.Text;
            ewmsc();
        }

        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            label13.Text = textBox18.Text;
            ewmsc();
        }


        private void textBox9_TextChanged(object sender, EventArgs e)
        {

            label18.Text ="数量："+ textBox9.Text;
            ewmsc();
        }

        //private void textBox9_TextChanged(object sender, EventArgs e)
        //{

        //    label18.Text = "包装数量：" + textBox9.Text;
        //    ewmsc();
        //}

        // 20251021

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            ewmsc();
            if (textBox9.Text == "") { MessageBox.Show("请输入包装数量！", "提示信息"); }
            else if (textBox19.Text == "") { MessageBox.Show("请选择打印份数！", "提示信息"); }
            else if (Convert.ToInt16(textBox19.Text) > 0)
            {
				// 弹出打印机选择对话框
				printDialog1.Document = printDocument1;
				if (printDialog1.ShowDialog() == DialogResult.OK)
				{
					for (int q1 = 0; q1 < Convert.ToInt16(textBox19.Text); q1++)
					{
						//设置打印时横向还是纵向
						//printDocument1.PrinterSettings.Copies = Convert.ToInt16(textBox10.Text);
						printDocument1.DefaultPageSettings.Landscape = false;
						printDocument1.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
						printDocument1.Print();
						dbconn db = new dbconn();
						//db.sqlcommand("update capMembership set cu_tmcs='" + (Convert.ToInt16(label46.Text) + 1).ToString() + "' where userid='" + label27.Text + "'");
						ewmsc();
					}
				}
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label7.Text = textBox17.Text;
        }


        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bddetail();
        }

   
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            bddetail();
        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
               textBox11.Text=dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
               textBox18.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
               textBox15.Text = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
               textBox17.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               textBox2.Text = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
               //label50.Text = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
               //label16.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               label7.Text = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               textBox4.Text = dataGridView2.SelectedRows[0].Cells[6].Value.ToString();
                label16.Text = textBox4.Text;
                //label17.Text = dataGridView2.SelectedRows[0].Cells[5].Value.ToString();
                ewmsc();
                bqxx();
            }

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            label15.Text = textBox11.Text;
        }

        private void label23_Click(object sender, EventArgs e)
        {

        }

        private void label25_Click(object sender, EventArgs e)
        {

        }

        private void label28_Click(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
